﻿using CoffeeShop.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace CoffeeShop.DAO
{
    public class AreaDao
    {
        private CFMSEntities _entities = new CFMSEntities();

        /// <summary>
        /// Get all areas in DB
        /// </summary>
        /// <returns></returns>
        public List<Area> GetListAreas()
        {
            try
            {
                var resultQuery = (from area in _entities.Areas
                                  where area.IsDelete == false
                                  select area).OrderBy(area => area.OfficeId);
                return resultQuery.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }         
        }

        /// <summary>
        /// Get an area by AreaId
        /// </summary>
        /// <param name="areaId">Area object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public Area GetAreaById(int areaId)
        {
            try
            {
                var resultQuery = (from area in _entities.Areas
                                  where area.IsDelete == false
                                        && area.AreaId == areaId
                                  select area).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Get list areas based on OfficeId
        /// </summary>
        /// <param name="officeId">OfficeId</param>
        /// <returns>List of area</returns>
        public List<Area> GetListAreasByOfficeId(int officeId)
        {
            try
            {
                var resultSelect = from area in _entities.Areas
                                   where area.OfficeId == officeId
                                         && area.IsDelete == false
                                   select area;
                return resultSelect.ToList();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Create an area
        /// </summary>
        /// <param name="areaId">Area object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool CreateArea(Area area)
        {
            try
            {
                _entities.Areas.Add(area);
                var resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            { 
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Update an area
        /// </summary>
        /// <param name="areaId">Area object</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool UpdateArea(Area area)
        {
            try
            {
                _entities.Entry(area).State = EntityState.Modified;
                int resultQuery = _entities.SaveChanges();
                if (resultQuery > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Delete an area
        /// </summary>
        /// <param name="areaId">AreaId</param>
        /// <returns>True: Successful --- False: Fail</returns>
        public bool DeleteArea(int areaId)
        {
            try
            {
                Area areaEnt = new Area();
                areaEnt = (from area in _entities.Areas
                                  where area.AreaId == areaId
                                  select area).FirstOrDefault();
                areaEnt.IsDelete = true;
                _entities.Entry(areaEnt).State = EntityState.Modified;
                var resultDelete = _entities.SaveChanges();
                if (resultDelete > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// Get an area by name and officeId
        /// </summary>
        /// <param name="areaName">Area Name</param>
        /// <param name="officeId">OfficeId</param>
        /// <returns>An Area object</returns>
        public Area GetAreaByNameAndOfficeId(string areaName, int officeId)
        {
            try
            {
                var selectArea = (from area in _entities.Areas
                                 where area.AreaName == areaName
                                       && area.OfficeId == officeId
                                 select area).FirstOrDefault();
                return selectArea;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// Check is exist an office which has the same name
        /// </summary>
        /// <param name="officeId"></param>
        /// <returns></returns>
        public bool IsExistedWhenUpdate(int officeId, string areaName, int areaId)
        {
            try
            {
                var resultQuery = (from area in _entities.Areas
                                   where area.AreaId != areaId
                                         && area.AreaName == areaName
                                         && area.OfficeId == officeId
                                   select area).FirstOrDefault();
                if (resultQuery != null)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return true;
            }
        }
    }
}